package com.emm.thirdapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.leagsoft.emm.sandbox.EMMSandboxUtil;
import com.leagsoft.emm.sandbox.container.DataContainer;
import com.leagsoft.emm.sandbox.container.FileContainer;

import java.io.IOException;
import java.io.InputStream;

public class SanboxActivity extends Activity implements View.OnClickListener{

    TextView filetext,sanbox_fileinfo,fileinfo,savefile,viewfile,viewsanbox,encriptfile;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sanbox);
        filetext = (TextView) findViewById(R.id.filetext);
        sanbox_fileinfo = (TextView) findViewById(R.id.sanbox_fileinfo);
        savefile = (TextView) findViewById(R.id.savefile);
        viewfile = (TextView) findViewById(R.id.viewfile);
        fileinfo = (TextView) findViewById(R.id.fileinfo);
        viewsanbox = (TextView) findViewById(R.id.viewsanbox);
        encriptfile = (TextView) findViewById(R.id.encriptfile);



        savefile.setOnClickListener(this);
        viewfile.setOnClickListener(this);
        viewsanbox.setOnClickListener(this);
        encriptfile.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if(id == R.id.savefile){
            DataContainer dataContainer = EMMSandboxUtil.getDataContainer(this,"test.txt",true);
            String text = filetext.getText().toString();
            dataContainer.put("text",text);
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
        }else if(id == R.id.viewfile){
            DataContainer dataContainer = EMMSandboxUtil.getDataContainer(this,"test.txt",true);
            String strText = dataContainer.getString("text");
            fileinfo.setText("解密文件内容："+strText);
            Toast.makeText(this,"获取解密内容成功",Toast.LENGTH_SHORT).show();

        }else if(id == R.id.viewsanbox){
            DataContainer dataContainer = EMMSandboxUtil.getDataContainer(this,"test.txt",false);
            String strText = dataContainer.getString("text");
            sanbox_fileinfo.setText("沙箱文件信息："+strText);
            Toast.makeText(this,"获取沙箱内容成功",Toast.LENGTH_SHORT).show();
        }else if(id == R.id.encriptfile){
            try {
                FileContainer fileContainer = EMMSandboxUtil.getFileContainer(getApplicationContext());
                InputStream inputStream = this.getAssets().open("test.docx");
                boolean isFlag = fileContainer.putFile(inputStream, "/temp/test.docx");
                if(isFlag){
                    Toast.makeText(this,"加密文件成功",Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            FileContainer fileContainer = EMMSandboxUtil.getFileContainer(getApplicationContext());
            fileContainer.getBytes("/temp/test.docx");

        }

    }
}
